﻿/**
<code>
  <author name="Zealic" email="zealic(at)gmail.com" type="Creator"/>
</code>
**/
using System;


namespace Zealib.Extensions
{
    /// <summary>
    /// Endian extender, In Big-Endian and Little-Endian conversion between(htons/htohs).
    /// </summary>
    public static class EndianExtender
    {   
        /// <summary>
        /// In Big-Endian and Little-Endian conversion between.
        /// </summary>
        /// <param name="self">To convert the value.</param>
        /// <returns>To converted the value.</returns>
        public static short Endian(this short self)
        {
            byte[] buffer = BitConverter.GetBytes(self);
            Array.Reverse(buffer);
            return BitConverter.ToInt16(buffer, 0);
        }

        /// <summary>
        /// In Big-Endian and Little-Endian conversion between.
        /// </summary>
        /// <param name="self">To convert the value.</param>
        /// <returns>To converted the value.</returns>
        public static ushort Endian(this ushort self)
        {
            byte[] buffer = BitConverter.GetBytes(self);
            Array.Reverse(buffer);
            return BitConverter.ToUInt16(buffer, 0);
        }

        /// <summary>
        /// In Big-Endian and Little-Endian conversion between.
        /// </summary>
        /// <param name="self">To convert the value.</param>
        /// <returns>To converted the value.</returns>
        public static int Endian(this int self)
        {
            byte[] buffer = BitConverter.GetBytes(self);
            Array.Reverse(buffer);
            return BitConverter.ToInt32(buffer, 0);
        }

        /// <summary>
        /// In Big-Endian and Little-Endian conversion between.
        /// </summary>
        /// <param name="self">To convert the value.</param>
        /// <returns>To converted the value.</returns>
        public static uint Endian(this uint self)
        {
            byte[] buffer = BitConverter.GetBytes(self);
            Array.Reverse(buffer);
            return BitConverter.ToUInt32(buffer, 0);
        }

        /// <summary>
        /// In Big-Endian and Little-Endian conversion between.
        /// </summary>
        /// <param name="self">To convert the value.</param>
        /// <returns>To converted the value.</returns>
        public static long Endian(this long self)
        {
            byte[] buffer = BitConverter.GetBytes(self);
            Array.Reverse(buffer);
            return BitConverter.ToInt64(buffer, 0);
        }

        /// <summary>
        /// In Big-Endian and Little-Endian conversion between.
        /// </summary>
        /// <param name="self">To convert the value.</param>
        /// <returns>To converted the value.</returns>
        public static ulong Endian(this ulong self)
        {
            byte[] buffer = BitConverter.GetBytes(self);
            Array.Reverse(buffer);
            return BitConverter.ToUInt64(buffer, 0);
        }
    }
}
